package com.abbas.smartCity.ui.findJob;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;

import com.abbas.smartCity.Death;
import com.abbas.smartCity.R;
import com.abbas.smartCity.adapter.BannerImageAdapter2;
import com.abbas.smartCity.adapter.ListCommAdapter;
import com.abbas.smartCity.base.BaseFragment;
import com.abbas.smartCity.bean.findJob.JobList;
import com.abbas.smartCity.bean.findJob.ProfessionList;
import com.abbas.smartCity.retroft.ICallback;
import com.abbas.smartCity.view.MGridView;
import com.abbas.smartCity.view.MListView;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.Arrays;
import java.util.List;

import retrofit2.Call;

public class FindJobFragment extends BaseFragment {

    private Banner<Integer, BannerImageAdapter2<Integer>> banner;
    private EditText searchEt;
    private MGridView gv;
    private MListView lv;
    private List<ProfessionList.RowsBean> professionListRows;
    private ListCommAdapter<JobList.RowsBean, ListCommAdapter.ParkingHistoryHolder> listAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_find_job;
    }

    @Override
    protected void initView() {
        banner = rootView.findViewById(R.id.banner);
        searchEt = (EditText) rootView.findViewById(R.id.search_et);
        gv = (MGridView) rootView.findViewById(R.id.gv);
        lv = (MListView) rootView.findViewById(R.id.lv);
    }

    @Override
    protected void initData() {
//        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
//                .setImageLoader(App.objLoader)
//                .setImages(Arrays.asList(R.drawable.findjob_sp01, R.drawable.findjob_sp02, R.drawable.findjob_sp03))
//                .startAutoPlay();

        banner.setAdapter(new BannerImageAdapter2<>(Arrays.asList(R.drawable.findjob_sp01, R.drawable.findjob_sp02, R.drawable.findjob_sp03)))
                .setIndicator(new CircleIndicator(requireContext()))
                .start();


        professionListRows = Death.professionList.getRows();
        gv.setAdapter(new ListCommAdapter<ProfessionList.RowsBean, ListCommAdapter.TextHolder>(
                professionListRows.subList(0, 8),
                ListCommAdapter.TextHolder.class,
                android.R.layout.simple_list_item_1
        ) {
            @Override
            protected void show(ProfessionList.RowsBean data, View convertView, TextHolder viewHolder, int position, Context context) {
                viewHolder.tv.setText(data.getProfessionName());
            }
        });
        getJobList(null);
    }

    @Override
    protected void initEvent() {
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final ProfessionList.RowsBean rowsBean = professionListRows.get(position);
                getJobList(rowsBean.getId());
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final JobList.RowsBean item = listAdapter.getItem(position);
                //TODO::::
                final Intent intent = new Intent(requireActivity(), JobDetailActivity.class);
                intent.putExtra("data", new Gson().toJson(item));
                startActivity(intent);
            }
        });
    }

    private void getJobList(Integer professionId) {
        api.getJobList(professionId).enqueue(new ICallback<JobList>() {
            @Override
            public void onSuccess(Call<JobList> call, JobList data) {
                if (listAdapter == null) {
                    listAdapter = new ListCommAdapter<JobList.RowsBean, ListCommAdapter.ParkingHistoryHolder>(
                            data.getRows(),
                            ListCommAdapter.ParkingHistoryHolder.class,
                            R.layout.parking_history_item
                    ) {
                        @Override
                        protected void show(JobList.RowsBean item, View convertView, ParkingHistoryHolder viewHolder, int position, Context context) {
                            viewHolder.name.setText(item.getName());
                            viewHolder.tag1.setText("岗位职责:" + item.getObligation());
                            viewHolder.tag2.setText("地址:" + item.getAddress());
                            viewHolder.tag3.setText("公司:" + item.getCompanyName());
                            viewHolder.money.setVisibility(View.GONE);
                        }
                    };
                    lv.setAdapter(listAdapter);
                } else {
                    listAdapter.resetData(data.getRows());
                }
            }
        });
    }
}